<?php
namespace plugins\goods\controller;

use cmf\controller\PluginRestBaseController;
use plugins\goods\model\GoodsSpecModel;
use think\Db;
use plugins\goods\model\GoodsModel;

class ApiGoodsSpecController extends PluginRestBaseController
{
    public function getGoodsSpec()
    {
        $param = $this->request->param();
        if (empty($param['gid'])) {
            return api_json_echo(false, '请输入商品id');
        }

        $good = (new GoodsModel)->get(['id' => $param['gid']]);
        //商品规格
        $spec = db("goods_spec_list")->field('id,attr,price,num,img,sku')->where(['gid' => $param['gid']])->select()->toArray();
        $attr_ids = [];
        //商品规格遍历
        foreach ($spec as $key_1 => $val_1) {
            $ids = explode('#*_*#',$val_1['attr']);
            $attr_ids = array_merge($attr_ids, $ids);
        }

        //得到商品分类的模板id
        // $spec_id = (new GoodsSpecModel)->get(['cid' => $good->spec_temp])->spec_id;
        $spec_id = $good->spec_temp;
        // dump($spec_id);
        // exit();
        // spec_name
        $spec_name = db('goods_spec_name')->field('name_id,spec_name')->where(['spec_id' => $spec_id])->select()->toArray();
        // spec_attr
        foreach ($spec_name as $key => &$value) {
            $spec_attr    = db('goods_spec_attr')
                            ->field('attr_id as id,attr_name as name')
                            ->where(['name_id' => $value['name_id'], 'attr_id' => ['in', array_unique($attr_ids)] ])
                            ->select()
                            ->toArray();
            $value['v']   = $spec_attr;
            $value["k_2"] = "s" . ($key + 1);
        }
        
        // spec_list
        //所属商品规格的数组
        // $spec = db("goods_spec_list")->field('id,attr,price,num,img,sku')->where(['gid' => $param['gid']])->select()->toArray();
        foreach ($spec as $key => &$value) {
            $value['num'] = (int) $value['num'];
            $tmp          = explode('#*_*#', $value['attr']);
            foreach ($tmp as $k => $v) {
                $kkk         = "s" . ($k + 1);
                $value[$kkk] = $v;
            }
            unset($value['attr']);
            if (!empty($value['img'])) {
                $value['img'] = zy_domain()  . $value['img'];
            }
        }
        $arr         = [];
        $arr['tree'] = $spec_name;
        $arr['list'] = $spec;
        return api_json_echo(true, '查询完成', $arr);
    }

    // public function getGoodsSpec()
    // {
    //     $param = $this->request->param();
    //     if (empty($param['gid'])) {
    //         return api_json_echo(false, '请输入商品id');
    //     }
    //     $good = (new GoodsModel)->get(['id' => $param['gid']]);

    //     $spec = db("goods_spec_list")->field('id,attr,price,num,img,sku')->where(['gid' => $param['gid']])->select()->toArray();

    //     $attr_ids = [];
    //     foreach ($spec as $key_1 => $val_1) {
    //         $ids = explode('#*_*#',$val_1['attr']);
    //         $attr_ids = array_merge($attr_ids, $ids);
    //     }
        
    //     //得到商品分类的模板id
    //     $spec_id = (new GoodsSpecModel)->get(['cid' => $good->catid])->spec_id;
    //     // spec_name
    //     $spec_name = db('goods_spec_name')->field('name_id,spec_name')->where(['spec_id' => $spec_id])->select()->toArray();
    //     // spec_attr
    //     foreach ($spec_name as $key => &$value) {
    //         $spec_attr    = db('goods_spec_attr')->field('attr_id as id,attr_name as name')->where(['name_id' => $value['name_id'], 'attr_id' => ['in', $attr_ids] ])->select()->toArray();
    //         $value['v']   = $spec_attr;
    //         $value["k_2"] = "s" . ($key + 1);
    //     }
    //     // spec_list
    //     //所属商品规格的数组
    //     // $spec = db("goods_spec_list")->field('id,attr,price,num,img,sku')->where(['gid' => $param['gid']])->select()->toArray();
    //     foreach ($spec as $key => &$value) {
    //         $value['num'] = (int) $value['num'];
    //         $tmp          = explode('-', $value['attr']);
    //         foreach ($tmp as $k => $v) {
    //             $kkk         = "s" . ($k + 1);
    //             $value[$kkk] = $v;
    //         }
    //         unset($value['attr']);
    //         if (!empty($value['img'])) {
    //             $value['img'] = zy_domain() . '/plugins/goods/view/image/' . $value['img'];
    //         }
    //     }
    //     $arr         = [];
    //     $arr['tree'] = $spec_name;
    //     $arr['list'] = $spec;
    //     return api_json_echo(true, '查询完成', $arr);
    // }    

}
